import request from '@/utils/request'

/**
 * 过滤字典翻译字段
 */
const filterEmptyParams = (form) => {
  let data = form
  for (let o in data) {
    if (o.indexOf('$') !== -1 || !data[o]) delete data[o]
  }
  return data
}

export async function requestDepartmentTreeList(data) {
  let ret = { success: false, data: [] }
  try {
    const rsp = await request({
      url: '/api/dept/tree',
      method: 'post',
      data: filterEmptyParams(data),
      loading: false,
    })
    if (rsp.success) {
      ret = { success: true, data: { listObject: rsp.data, total: undefined } }
    }
  } catch (error) {
    console.log(error)
  }

  return ret
}

export function requestDepartmentCreate(data) {
  return request({
    url: '/api/dept/add',
    method: 'post',
    data: data,
  })
}
export function requestDepartmentUpdate(data) {
  return request({
    url: `/api/dept/update`,
    method: 'post',
    data: data,
  })
}
export function requestDepartmentDelete(params) {
  return request({
    url: `/api/dept/deldete`,
    method: 'post',
    data: { deptId: params.deptId, pid: params.pid },
  })
}

export function getDeptTree(pid) {
  return request({
    url: 'api/dept/childNodes/' + pid,
    method: 'post',
    loading: true,
  })
}

export function treeByDeptId(deptId) {
  return request({
    url: '/api/dept/nodesByDeptId/' + deptId,
    method: 'post',
  })
}

export function loadTable(data) {
  return request({
    url: 'api/dept/search',
    method: 'post',
    data,
    loading: false,
  })
}
